package com.qeegoo.b2bautozityp.moudle.detail.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.qeegoo.b2bautozityp.R;
import com.qeegoo.b2bautozityp.moudle.detail.bean.DetailBean;
import com.qeegoo.b2bautozityp.moudle.detail.view.OrderDetailsActivity;

import java.util.List;

/**
 * Created by HongQian.Wang on 2017/10/19.
 */

class SellAdapter extends BaseAdapter {

    private int leftPosition;
    private List<DetailBean.GoodsInfoMoreListBean.AskPriceQuoteGoodsListBean> mDataList;

    private List<DetailBean.GoodsInfoMoreListBean.AskPriceQuoteGoodsListBean>  mSelectedDataList;
    private List<String>  mSelectedList;  //选中的商品List 全局只有一个

    private ExpandAdapter expandAdapter;

    private String productId;
    private PageAdapter pageAdapter;

    public SellAdapter(PageAdapter pageAdapter, List<DetailBean.GoodsInfoMoreListBean.AskPriceQuoteGoodsListBean> dataList, ExpandAdapter expandAdapter, List<String> selectedList, List<DetailBean.GoodsInfoMoreListBean.AskPriceQuoteGoodsListBean> selectedDataList) {
             this.mSelectedList=selectedList;
             this.mSelectedDataList=selectedDataList;
             this.pageAdapter=pageAdapter;
              this.mDataList=dataList;
             this.expandAdapter=expandAdapter;
    }

    @Override
    public int getCount() {
        return mDataList==null?0:mDataList.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }



    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        View inflate = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cell_sell, viewGroup, false);
      ToggleButton toggle_add = (ToggleButton) inflate.findViewById(R.id.toggle_add);
        TextView tv_price = (TextView) inflate.findViewById(R.id.tv_price);
        TextView tv_amount = (TextView) inflate.findViewById(R.id.tv_amount);
        TextView tv_goods_type = (TextView) inflate.findViewById(R.id.tv_goods_type);
        TextView tv_goods_name = (TextView) inflate.findViewById(R.id.tv_goods_name);
        DetailBean.GoodsInfoMoreListBean.AskPriceQuoteGoodsListBean bean = mDataList.get(position);

        tv_goods_name.setText(bean.getGoodsName());
        tv_price.setText("￥"+bean.getPrice());

        tv_amount.setText("库存"+bean.getAvlNum());
        tv_goods_type.setText(bean.getGoodsTypeStr());

        for (String s : mSelectedList){
               if((productId+bean.getGoodsId()).equals(s)) {
                   toggle_add.setChecked(true);
                   break;
               }
        }

        toggle_add.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                LeftAdapter leftAdapter = ((OrderDetailsActivity) (viewGroup.getContext())).getLeftAdapter();

                if(isChecked) {
                    mSelectedList.add(productId+bean.getGoodsId()+"");
                    mSelectedDataList.add(bean);
                    leftAdapter.addSelectedArrow(leftPosition);
                }else {
                    leftAdapter.reduceSelectedArrow(leftPosition);
                    mSelectedList.remove(productId+ bean.getGoodsId()+"");
                    mSelectedDataList.remove(bean);
                }

                Log.e("选中的数据",mSelectedList.toString());
                Log.e("选中的数据",mSelectedDataList.toString());
                //
                expandAdapter.setSelectedDataAndRefreshed(mSelectedList,mSelectedDataList);

            }
        });
        return inflate;
    }



    public void setProductId(String productId) {
                this.productId=String.valueOf(productId);
    }


    public void setDataAndRefreshed(int leftPosition, List<DetailBean.GoodsInfoMoreListBean.AskPriceQuoteGoodsListBean> mDataList){
        this.leftPosition=leftPosition;
         this.mDataList=mDataList;
         notifyDataSetChanged();
    }


}
